package 爆刷leetcode.balabala.删除有序数组中的重复项26;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author yaya
 * @Create 2024/5/25 14:33
 * Description:
 */
public class RemoveDuplicates {
    public static void main(String[] args) {
        int[] nums = {1, 2, 2};
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i],(map.get(nums[i])) == null ? 1 : (map.get(nums[i]) + 1));
        }

        System.out.println(map);
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
           if (entry.getValue() > (nums.length / 2)) System.out.println(entry);
        }
    }
}

class Solution {
    public int removeDuplicates(int[] nums) {
        // 快慢指针
        // int fast = 0;
        int slow = 0;
        int count = 1;

        for (int fast = 0; fast < nums.length; fast++) {
            // 慢指针指向
            // 如果快慢指针数字相同
            if (nums[slow] == nums[fast]) continue;

            slow++;
            nums[slow] = nums[fast];
            count++;
        }

        return count;
    }

    // 保留两个相同的数，如果有的话
    public int removeDuplicatesPlus(int[] nums) {
        int n = nums.length;
        if (n <= 2) {
            return n;
        }
        int slow = 2, fast = 2;
        // [0,0,1,1,1,1,2,3,3]
        while (fast < n) {
            if (nums[slow - 2] != nums[fast]) {
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        return slow;
    }
}